define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'sport/score/index' + location.search,
                    add_url: 'sport/score/add',
                    edit_url: 'sport/score/edit',
                    del_url: 'sport/score/del',
                    multi_url: 'sport/score/multi',
                    import_url: 'sport/score/import',
                    dragsort_url: '',
                    table: 'sport_score',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'area.area_name', title: __('Sport_area_id'), operate: false},
                        {field: 'score_type', title: __('Score_type'), searchList: {"middle":__('Score_type middle'),"senior":__('Score_type senior')}, formatter: Table.api.formatter.normal},
                        {field: 'sex', title: __('Sex'), searchList: {"1":__('Sex 1'),"2":__('Sex 2')}, formatter: Table.api.formatter.normal},
                        {field: 'stype.type_name', title: __('Sport_type_id'), operate: false},
                        {field: 'project_name', title: __('Project_name'), operate: 'LIKE'},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'show_unit', title: __('Show_unit'), operate: false},
                        {field: 'unit', title: __('Unit'), searchList: {"ms":__('Unit ms'),"meter":__('Unit meter'),"cmeter":__('Unit cmeter'),"sec":__('Unit sec'),"an":__('Unit an')}, formatter: Table.api.formatter.normal},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Status normal'),"hidden":__('Status hidden')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {   
                          field: 'operate', 
                          title: __('Operate'), 
                          table: table, 
                          events: Table.api.events.operate, 
                          buttons: [
                              {   
                                  name: 'feedback',
                                  text: __('自动反馈设置'),
                                  title: function (row) {
                                    return __(row.project_name + '自动反馈设置')
                                  },
                                  classname: 'btn btn-xs btn-success btn-magic btn-dialog',
                                  icon: 'fa fa-magic',
                                  url: function (row) {
                                      return Fast.api.fixurl('sport/feedback?sport_score_id=' + row.id)
                                  },
                                  extend: 'data-area=\'["80%","80%"]\'',
                              },  
                              {   
                                  name: 'feedback',
                                  text: __('导入评分标准'),
                                  title: function (row) {
                                    return __(row.project_name + '导入评分标准')
                                  },
                                  classname: 'btn btn-xs btn-success btn-import btn-dialog',
                                  icon: 'fa fa-import',
                                  url: function (row) {
                                      return Fast.api.fixurl('sport/score/import?sport_score_id=' + row.id)
                                  },
                                  callback: function (ret) {
                                      // $(".btn-refresh").trigger("click");//刷新当前页面的数据
                                      // ret.code ret.data ret.msg
                                      console.log('ret =>', ret)
                                      if (ret.code === 1) {
                                          var value = ret.data
                                          var msg = value.msg + '<br />';
                                          var err = value.err;
                                          for(var i in err) {
                                              msg = msg + err[i] + '<br />';
                                          }
                                          Layer.alert(msg, {title: "导入结果提示"});
                                      } else {
                                          Backend.api.toastr.error(ret.msg);
                                      }   
                                  },
                                  // extend: 'data-area=\'["80%","80%"]\'',
                              },  
                          ],  

                          formatter: Table.api.formatter.operate

                        }
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(document).on("change", "#c-sport_area_id", function(e){
                Fast.api.ajax({
                   url:'ajax/sportType',
                   data:{pid:$("#c-sport_area_id").val()}
                }, function(data, ret){
                   //成功的回调
                   
                   $("#c-sport_type_id").empty();
                   $("#c-sport_type_id").append("<option value=0>无</option>");
                   for(var i in data){
                      $("#c-sport_type_id").append("<option value="+ data[i]['id']+">"+ data[i]['name']+"</option>");
                   };
                   return false;
                }, function(data, ret){
                   //失败的回调
                   return false;
                });
            });
            Controller.api.bindevent();
        },
        edit: function () {
            $(document).on("change", "#c-sport_area_id", function(e){
                Fast.api.ajax({
                   url:'ajax/sportType',
                   data:{pid:$("#c-sport_area_id").val()}
                }, function(data, ret){
                   //成功的回调
                   $("#c-sport_type_id").empty();
                   $("#c-sport_type_id").append("<option value=0>无</option>");
                   for(var i in data){
                      $("#c-sport_type_id").append("<option value="+ data[i]['id']+">"+ data[i]['name']+"</option>");
                   };
                   return false;
                }, function(data, ret){
                   //失败的回调
                   return false;
                });
            });
            Controller.api.bindevent();
        },
        import: function () {
            $(document).on("click", ".btn-default", function() {
                var templateFile = $("#template_file").val();
                window.open(templateFile);
            })
            $(document).on("click", ".btn-primary", function () {
                var personFile = $("#c-person_file").val();
                var scoreId = $("#score_id").val();
                if (personFile == '') {
                    Backend.api.toastr.error('请选择导入的文件');
                    return
                }   
                if (scoreId == '') {
                    Backend.api.toastr.error('缺少参数');
                    return
                }
                const params = Object.assign({})
                params.file = personFile 
                params.score_id = scoreId
                console.log('params =>', params)
                $.ajax({
                    url: `sport/score/import`,
                    type: 'post',
                    dataType: 'json',
                    data: params,
                    success: function (ret) {
                        if (ret.hasOwnProperty("code")) {
                            var data = ret.hasOwnProperty("data") && ret.data != "" ? ret.data : ""; 
                            Fast.api.close(ret);
                        }   
                    }, error: function (e) {
                        console.log(e.message)
                        Backend.api.toastr.error('导入失败');
                    }   
                }); 
            })  
            $("#faupload-person").data("upload-success", function(data, ret, up, file){
                console.log('data =>', data)
                console.log('ret =>', ret)
                console.log('up =>', up) 
                console.log('file =>', file)
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
